home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 6 / QRZ Ham Radio Callsign Database - Volume 6.iso / pc / files / dsp / dspkgctr.z / dspkgctr / gcc / stupid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-08  |  16.0 KB  |  538 lines

  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4.    $Id: stupid.c,v 1.3 91/10/10 14:47:00 jeff Exp $
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* This file performs stupid register allocation, which is used
  24.    when cc1 gets the -noreg switch (which is when cc does not get -O).
  25.  
  26.    Stupid register allocation goes in place of the the flow_analysis,
  27.    local_alloc and global_alloc passes.  combine_instructions cannot
  28.    be done with stupid allocation because the data flow info that it needs
  29.    is not computed here.
  30.  
  31.    In stupid allocation, the only user-defined variables that can
  32.    go in registers are those declared "register".  They are assumed
  33.    to have a life span equal to their scope.  Other user variables
  34.    are given stack slots in the rtl-generation pass and are not
  35.    represented as pseudo regs.  A compiler-generated temporary
  36.    is assumed to live from its first mention to its last mention.
  37.  
  38.    Since each pseudo-reg's life span is just an interval, it can be
  39.    represented as a pair of numbers, each of which identifies an insn by
  40.    its position in the function (number of insns before it).  The first
  41.    thing done for stupid allocation is to compute such a number for each
  42.    insn.  It is called the suid.  Then the life-interval of each
  43.    pseudo reg is computed.  Then the pseudo regs are ordered by priority
  44.    and assigned hard regs in priority order.  */
  45.  
  46. #include <stdio.h>
  47. #include "config.h"
  48. #include "rtl.h"
  49. #if ! defined( _INTELC32_ )
  50. #include "hard-reg-set.h"
  51. #else
  52. #include "hardrset.h"
  53. #endif
  54. #include "regs.h"
  55.  
  56. /* Vector mapping INSN_UIDs to suids.
  57.    The suids are like uids but increase monononically always.
  58.    We use them to see whether a subroutine call came
  59.    between a variable's birth and its death.  */
  60.  
  61. static int *uid_suid;
  62.  
  63. /* Get the suid of an insn.  */
  64.  
  65. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  66.  
  67. /* Record the suid of the last CALL_INSN
  68.    so we can tell whether a pseudo reg crosses any calls.  */
  69.  
  70. static int last_call_suid;
  71.  
  72. /* Record the suid of the last JUMP_INSN
  73.    so we can tell whether a pseudo reg crosses any jumps.  */
  74.  
  75. static int last_jump_suid;
  76.  
  77. /* Record the suid of the last CODE_LABEL
  78.    so we can tell whether a pseudo reg crosses any labels.  */
  79.  
  80. static int last_label_suid;
  81.  
  82. /* Element N is suid of insn where life span of pseudo reg N ends.
  83.    Element is  0 if register N has not been seen yet on backward scan.  */
  84.  
  85. static int *reg_where_dead;
  86.  
  87. /* Element N is suid of insn where life span of pseudo reg N begins.  */
  88.  
  89. static int *reg_where_born;
  90.  
  91. /* Element N is 1 if pseudo reg N lives across labels or jumps.  */
  92.  
  93. static char *reg_crosses_blocks;
  94.  
  95. /* Numbers of pseudo-regs to be allocated, highest priority first.  */
  96.  
  97. static int *reg_order;
  98.  
  99. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  100.    at the current point in the instruction stream.  */
  101.  
  102. static char *regs_live;
  103.  
  104. /* Indexed by insn's suid, the set of hard regs live after that insn.  */
  105.  
  106. static HARD_REG_SET *after_insn_hard_regs;
  107.  
  108. /* Record that hard reg REGNO is live after insn INSN.  */
  109.  
  110. #define MARK_LIVE_AFTER(INSN,REGNO)  \
  111.   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  112.  
  113. static void stupid_mark_refs ();
  114. static int stupid_reg_compare ();
  115. static int stupid_find_reg ();
  116.  
  117. /* Stupid life analysis is for the case where only variables declared
  118.    `register' go in registers.  For this case, we mark all
  119.    pseudo-registers that belong to register variables as
  120.    dying in the last instruction of the function, and all other
  121.    pseudo registers as dying in the last place they are referenced.
  122.    Hard registers are marked as dying in the last reference before
  123.    the end or before each store into them.  */
  124.  
  125. void
  126. stupid_life_analysis (f, nregs, file)
  127.      rtx f;
  128.      int nregs;
  129.      FILE *file;
  130. {
  131.   register int i;
  132.   register rtx last, insn;
  133.   int max_uid;
  134.  
  135.   bzero (regs_ever_live, sizeof regs_ever_live);
  136.  
  137.   regs_live = (char *) alloca (nregs);
  138.  
  139.   /* First find the last real insn, and count the number of insns,
  140.      and assign insns their suids.  */
  141.  
  142.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  143.     if (INSN_UID (insn) > i)
  144.       i = INSN_UID (insn);
  145.  
  146.   max_uid = i + 1;
  147.   uid_suid = (int *) alloca ((i + 1) * sizeof (int));
  148.  
  149.   /* Compute the mapping from uids to suids.
  150.      Suids are numbers assigned to insns, like uids,
  151.      except that suids increase monotonically through the code.  */
  152.  
  153.   last = 0;            /* In case of empty function body */
  154.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  155.     {
  156.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  157.       || GET_CODE (insn) == JUMP_INSN)
  158.     last = insn;
  159.       INSN_SUID (insn) = ++i;
  160.     }
  161.  
  162.   last_call_suid = i + 1;
  163.   last_jump_suid = i + 1;
  164.   last_label_suid = i + 1;
  165.  
  166.   max_regno = nregs;
  167.  
  168.   /* Allocate tables to record info about regs.  */
  169.  
  170.   reg_where_dead = (int *) alloca (nregs * sizeof (int));
  171.   bzero (reg_where_dead, nregs * sizeof (int));
  172.  
  173.   reg_where_born = (int *) alloca (nregs * sizeof (int));
  174.   bzero (reg_where_born, nregs * sizeof (int));
  175.  
  176.   reg_crosses_blocks = (char *) alloca (nregs);
  177.   bzero (reg_crosses_blocks, nregs);
  178.  
  179.   reg_order = (int *) alloca (nregs * sizeof (int));
  180.   bzero (reg_order, nregs * sizeof (int));
  181.  
  182.   reg_renumber = (short *) oballoc (nregs * sizeof (short));
  183.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  184.     reg_renumber[i] = i;
  185.  
  186.   after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  187.   bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  188.  
  189.   /* Allocate and zero out many data structures
  190.      that will record the data from lifetime analysis.  */
  191.  
  192.   allocate_for_life_analysis ();
  193.  
  194.   for (i = 0; i < max_regno; i++)
  195.     {
  196.       reg_n_deaths[i] = 1;
  197.     }
  198.  
  199.   bzero (regs_live, nregs);
  200.  
  201.   /* Find where each pseudo register is born and dies,
  202.      by scanning all insns from the end to the start
  203.      and noting all mentions of the registers.
  204.  
  205.      Also find where each hard register is live
  206.      and record that info in after_insn_hard_regs.
  207.      regs_live[I] is 1 if hard reg I is live
  208.      at the current point in the scan.  */
  209.  
  210.   for (insn = last; insn; insn = PREV_INSN (insn))
  211.     {
  212.       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  213.  
  214.       /* Copy the info in regs_live
  215.      into the element of after_insn_hard_regs
  216.      for the current position in the rtl code.  */
  217.  
  218.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  219.     if (regs_live[i])
  220.       SET_HARD_REG_BIT (*p, i);
  221.  
  222.       /* Mark all call-clobbered regs as live after each call insn
  223.      so that a pseudo whose life span includes this insn
  224.      will not go in one of them.
  225.      Then mark those regs as all dead for the continuing scan
  226.      of the insns before the call.  */
  227.  
  228.       if (GET_CODE (insn) == CALL_INSN)
  229.     {
  230.       last_call_suid = INSN_SUID (insn);
  231.       IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  232.                 call_used_reg_set);
  233.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  234.         if (call_used_regs[i])
  235.           regs_live[i] = 0;
  236.     }
  237.  
  238.       if (GET_CODE (insn) == JUMP_INSN)
  239.     last_jump_suid = INSN_SUID (insn);
  240.  
  241.       if (GET_CODE (insn) == CODE_LABEL)
  242.     last_label_suid = INSN_SUID (insn);
  243.  
  244.       /* Update which hard regs are currently live
  245.      and also the birth and death suids of pseudo regs
  246.      based on the pattern of this insn.  */
  247.  
  248.       if (GET_CODE (insn) == INSN
  249.       || GET_CODE (insn) == CALL_INSN
  250.       || GET_CODE (insn) == JUMP_INSN)
  251.     {
  252.       stupid_mark_refs (PATTERN (insn), insn);
  253.     }
  254.     }
  255.  
  256.   /* Now decide the order in which to allocate the pseudo registers.  */
  257.  
  258.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  259.     reg_order[i] = i;
  260.  
  261.   qsort (®_order[FIRST_PSEUDO_REGISTER],
  262.      max_regno - FIRST_PSEUDO_REGISTER, sizeof (int),
  263.      stupid_reg_compare);
  264.  
  265.   /* Now, in that order, try to find hard registers for those pseudo regs.  */
  266.  
  267.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  268.     {
  269.       register int r = reg_order[i];
  270.       enum reg_class class;
  271.  
  272.       /* Some regnos disappear from the rtl.  Ignore them to avoid crash.  */
  273.       if (regno_reg_rtx[r] == 0)
  274.     continue;
  275.  
  276.       /* Now find the best hard-register class for this pseudo register */
  277.       if (N_REG_CLASSES > 1)
  278.     {
  279.       class = reg_preferred_class (r);
  280.  
  281.       reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r], class,
  282.                          PSEUDO_REGNO_MODE (r),
  283.                          reg_where_born[r],
  284.                          reg_where_dead[r],
  285.                          reg_crosses_blocks[r]);
  286.     }
  287.       else
  288.     reg_renumber[r] = -1;
  289.  
  290.       /* If no reg available in that class,
  291.      try any reg.  */
  292.       if (reg_renumber[r] == -1)
  293.     reg_renumber[r] = stupid_find_reg (reg_n_calls_crossed[r],
  294.                        GENERAL_REGS,
  295.                        PSEUDO_REGNO_MODE (r),
  296.                        reg_where_born[r],
  297.                        reg_where_dead[r],
  298.                        reg_crosses_blocks[r]);
  299.     }
  300.  
  301.   if (file)
  302.     dump_flow_info (file);
  303. }
  304.  
  305. /* Comparison function for qsort.
  306.    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
  307.  
  308. static int
  309. stupid_reg_compare (r1p, r2p)
  310.      int *r1p, *r2p;
  311. {
  312.   register int r1 = *r1p, r2 = *r2p;
  313.   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  314.   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  315.   int tem;
  316.  
  317.   tem = len2 - len1;
  318.   if (tem != 0) return tem;
  319.  
  320.   tem = reg_n_refs[r1] - reg_n_refs[r2];
  321.   if (tem != 0) return tem;
  322.  
  323.   /* If regs are equally good, sort by regno,
  324.      so that the results of qsort leave nothing to chance.  */
  325.   return r1 - r2;
  326. }
  327.  
  328. /* Find a block of SIZE words of hard registers in reg_class CLASS
  329.    that can hold a value of machine-mode MODE
  330.      (but actually we test only the first of the block for holding MODE)
  331.    currently free from after insn whose suid is BIRTH
  332.    through the insn whose suid is DEATH,
  333.    and return the number of the first of them.
  334.    Return -1 if such a block cannot be found.
  335.  
  336.    If CALL_PRESERVED is nonzero, insist on registers preserved
  337.    over subroutine calls, and return -1 if cannot find such.
  338.    If CROSSES_BLOCKS is nonzero, reject registers for which
  339.    PRESERVE_DEATH_INFO_REGNO_P is true.  */
  340.  
  341. static int
  342. stupid_find_reg (call_preserved, class, mode,
  343.          born_insn, dead_insn, crosses_blocks)
  344.      int call_preserved;
  345.      enum reg_class class;
  346.      enum machine_mode mode;
  347.      int born_insn, dead_insn;
  348.      int crosses_blocks;
  349. {
  350.   register int i, ins;
  351. #ifdef HARD_REG_SET
  352.   register        /* Declare them register if they are scalars.  */
  353. #endif
  354.     HARD_REG_SET used, this_reg;
  355.  
  356.   COPY_HARD_REG_SET (used,
  357.              call_preserved ? call_used_reg_set : fixed_reg_set);
  358.  
  359. #if defined( DSP56000 ) || defined( DSP96000 ) || defined( DSP56100 )
  360.   /* we want to take into account the insn in which the register dies, 
  361.      because it might be a destination in that insn. (such as a CLOBBER). */
  362.   for (ins = born_insn; ins <= dead_insn; ins++)
  363. #else
  364.   for (ins = born_insn; ins < dead_insn; ins++)
  365. #endif
  366.     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  367.  
  368.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  369.  
  370.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  371.     {
  372. #ifdef REG_ALLOC_ORDER
  373.       int regno = reg_alloc_order[i];
  374. #else
  375.       int regno = i;
  376. #endif
  377.  
  378.       /* If we need reasonable death info on this hard reg,
  379.      don't use it for anything whose life spans a label or a jump.  */
  380. #ifdef PRESERVE_DEATH_INFO_REGNO_P
  381.       if (PRESERVE_DEATH_INFO_REGNO_P (regno)
  382.       && crosses_blocks)
  383.     continue;
  384. #endif
  385.       if (! TEST_HARD_REG_BIT (used, regno)
  386.       && HARD_REGNO_MODE_OK (regno, mode))
  387.     {
  388.       register int j;
  389.       register int size1 = HARD_REGNO_NREGS (regno, mode);
  390.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
  391.       if (j == size1)
  392.         {
  393.           CLEAR_HARD_REG_SET (this_reg);
  394.           while (--j >= 0)
  395.         SET_HARD_REG_BIT (this_reg, regno + j);
  396. #if defined( DSP56000 ) || defined( DSP96000 ) || defined( DSP56100 )
  397.   /* we want to take into account the insn in which the register dies, 
  398.      because it might be a destination in that insn. (such as a CLOBBER). */
  399.           for (ins = born_insn; ins <= dead_insn; ins++)
  400. #else
  401.           for (ins = born_insn; ins < dead_insn; ins++)
  402. #endif
  403.         {
  404.           IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  405.         }
  406.           return regno;
  407.         }
  408. #ifndef REG_ALLOC_ORDER
  409.       i += j;            /* Skip starting points we know will lose */
  410. #endif
  411.     }
  412.     }
  413.   return -1;
  414. }
  415.  
  416. /* Walk X, noting all assignments and references to registers
  417.    and recording what they imply about life spans.
  418.    INSN is the current insn, supplied so we can find its suid.  */
  419.  
  420. static void
  421. stupid_mark_refs (x, insn)
  422.      rtx x, insn;
  423. {
  424.   register RTX_CODE code = GET_CODE (x);
  425.   register char *fmt;
  426.   register int regno, i;
  427.  
  428.   if (code == SET || code == CLOBBER)
  429.     {
  430.       if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  431.     {
  432.       /* Register is being assigned.  */
  433.       regno = REGNO (SET_DEST (x));
  434.  
  435.       /* For hard regs, update the where-live info.  */
  436.       if (regno < FIRST_PSEUDO_REGISTER)
  437.         {
  438.           register int j
  439.         = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  440.           while (--j >= 0)
  441.         {
  442.           regs_ever_live[regno+j] = 1;
  443.           regs_live[regno+j] = 0;
  444.           /* The following line is for unused outputs;
  445.              they do get stored even though never used again.  */
  446.           MARK_LIVE_AFTER (insn, regno);
  447.           /* When a hard reg is clobbered, mark it in use
  448.              just before this insn, so it is live all through.  */
  449.           if (code == CLOBBER)
  450.             SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (insn)],
  451.                       regno);
  452.         }
  453.         }
  454.       /* For pseudo regs, record where born, where dead, number of
  455.          times used, and whether live across a call.  */
  456.       else
  457.         {
  458.           /* Update the life-interval bounds of this pseudo reg.  */
  459.  
  460.           /* When a pseudo-reg is CLOBBERed, it is born just before
  461.          the clobbering insn.  When setting, just after.  */
  462.           int where_born = INSN_SUID (insn) - (code == CLOBBER);
  463.  
  464.           reg_where_born[regno] = where_born;
  465.           /* The reg must live at least one insn even
  466.          in it is never again used--because it has to go
  467.          in SOME hard reg.  */
  468.           if (reg_where_dead[regno] < where_born + 1)
  469.         reg_where_dead[regno] = where_born + 1;
  470.  
  471.           /* Count the refs of this reg.  */
  472.           reg_n_refs[regno]++;
  473.  
  474.           if (last_call_suid < reg_where_dead[regno])
  475.         reg_n_calls_crossed[regno] += 1;
  476.           if (last_jump_suid < reg_where_dead[regno]
  477.           || last_label_suid < reg_where_dead[regno])
  478.         reg_crosses_blocks[regno] = 1;
  479.         }
  480.     }
  481.       /* Record references from the value being set,
  482.      or from addresses in the place being set if that's not a reg.
  483.      If setting a SUBREG, we treat the entire reg as *used*.  */
  484.       if (code == SET)
  485.     {
  486.       stupid_mark_refs (SET_SRC (x), insn);
  487.       if (GET_CODE (SET_DEST (x)) != REG)
  488.         stupid_mark_refs (SET_DEST (x), insn);
  489.     }
  490.       return;
  491.     }
  492.  
  493.   /* Register value being used, not set.  */
  494.  
  495.   if (code == REG)
  496.     {
  497.       regno = REGNO (x);
  498.       if (regno < FIRST_PSEUDO_REGISTER)
  499.     {
  500.       /* Hard reg: mark it live for continuing scan of previous insns.  */
  501.       register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  502.       while (--j >= 0)
  503.         {
  504.           regs_ever_live[regno+j] = 1;
  505.           regs_live[regno+j] = 1;
  506.         }
  507.     }
  508.       else
  509.     {
  510.       /* Pseudo reg: record first use, last use and number of uses.  */
  511.  
  512.       reg_where_born[regno] = INSN_SUID (insn);
  513.       reg_n_refs[regno]++;
  514.       if (regs_live[regno] == 0)
  515.         {
  516.           regs_live[regno] = 1;
  517.           reg_where_dead[regno] = INSN_SUID (insn);
  518.         }
  519.     }
  520.       return;
  521.     }
  522.  
  523.   /* Recursive scan of all other rtx's.  */
  524.  
  525.   fmt = GET_RTX_FORMAT (code);
  526.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  527.     {
  528.       if (fmt[i] == 'e')
  529.     stupid_mark_refs (XEXP (x, i), insn);
  530.       if (fmt[i] == 'E')
  531.     {
  532.       register int j;
  533.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  534.         stupid_mark_refs (XVECEXP (x, i, j), insn);
  535.     }
  536.     }
  537. }
  538.